<template>
	<view class="favorite-posts-page">
		<view v-if="!isLogin" class="login-tip">
			<text class="tip-text">请先登录查看收藏的帖子</text>
			<button class="login-btn" @click="goLogin">去登录</button>
		</view>
		
		<view v-else>
			<view v-if="posts.length === 0" class="empty">
				<text class="empty-text">还没有收藏任何帖子</text>
			</view>
			
			<scroll-view scroll-y class="posts-container" :show-scrollbar="false">
				<view class="posts-grid">
					<view 
						v-for="post in posts" 
						:key="post.id" 
						class="post-card"
						@click="viewDetail(post.id)"
					>
						<image 
							class="post-image" 
							:src="getImageUrl(post.imageUrl)" 
							mode="aspectFill" 
						/>
						<view class="post-info">
							<text class="post-title">{{ post.title }}</text>
							<view class="post-meta">
								<text class="post-author">{{ post.authorName }}</text>
								<text class="post-time">{{ formatTime(post.createdAt) }}</text>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
import { api } from '../../utils/request.js'
import { BASE_URL } from '../../utils/server.js'

export default {
	data() {
		return {
			isLogin: false,
			posts: []
		}
	},
	onLoad() {
		this.checkLogin()
	},
	onShow() {
		if (this.isLogin) {
			this.loadFavoritePosts()
		}
	},
	methods: {
		checkLogin() {
			this.isLogin = !!uni.getStorageSync('token')
			if (this.isLogin) {
				this.loadFavoritePosts()
			}
		},
		async loadFavoritePosts() {
			try {
				const res = await api.getFavoritePosts()
				if (res.code === 0) {
					this.posts = res.data || []
				} else {
					if (res.code === 401) {
						this.isLogin = false
						uni.removeStorageSync('token')
					}
					this.posts = []
				}
			} catch (e) {
				console.error('加载收藏失败', e)
				this.posts = []
			}
		},
		goLogin() {
			uni.switchTab({
				url: '/pages/profile/index'
			})
		},
		viewDetail(id) {
			uni.navigateTo({
				url: `/pages/community/detail?id=${id}`
			})
		},
		getImageUrl(url) {
			if (!url) return '/static/logo.png'
			if (url.startsWith('http')) return url
			return BASE_URL + url
		},
		formatTime(timeStr) {
			if (!timeStr) return ''
			const date = new Date(timeStr)
			const now = new Date()
			const diff = now - date
			
			if (diff < 60000) return '刚刚'
			if (diff < 3600000) return Math.floor(diff / 60000) + '分钟前'
			if (diff < 86400000) return Math.floor(diff / 3600000) + '小时前'
			if (diff < 604800000) return Math.floor(diff / 86400000) + '天前'
			
			const year = date.getFullYear()
			const month = String(date.getMonth() + 1).padStart(2, '0')
			const day = String(date.getDate()).padStart(2, '0')
			return `${year}-${month}-${day}`
		}
	}
}
</script>

<style>
.favorite-posts-page {
	position: relative;
	height: 100vh;
	background: #f5f5f5;
}

.login-tip {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	height: 100vh;
	padding: 40rpx;
}

.tip-text {
	font-size: 28rpx;
	color: #999;
	margin-bottom: 40rpx;
}

.login-btn {
	background: linear-gradient(135deg, #39c5bb, #54e0c7);
	color: #fff;
	border-radius: 20rpx;
	padding: 20rpx 60rpx;
	font-size: 28rpx;
	border: none;
}

.empty {
	display: flex;
	align-items: center;
	justify-content: center;
	height: 100vh;
}

.empty-text {
	font-size: 28rpx;
	color: #999;
}

.posts-container {
	height: 100%;
	padding: 20rpx 20rpx 20rpx 20rpx;
	box-sizing: border-box;
}

.posts-grid {
	display: grid;
	grid-template-columns: 1fr 1fr;
	gap: 20rpx;
}

.post-card {
	background: #fff;
	border-radius: 16rpx;
	overflow: hidden;
	box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);
}

.post-image {
	width: 100%;
	height: 300rpx;
	background: #f0f0f0;
}

.post-info {
	padding: 16rpx;
}

.post-title {
	font-size: 28rpx;
	color: #333;
	font-weight: 500;
	display: -webkit-box;
	-webkit-box-orient: vertical;
	-webkit-line-clamp: 2;
	overflow: hidden;
	margin-bottom: 12rpx;
}

.post-meta {
	display: flex;
	justify-content: space-between;
	align-items: center;
	font-size: 22rpx;
	color: #999;
}

.post-author {
	flex: 1;
}

.post-time {
	flex-shrink: 0;
}
</style>

